/**
 * 打开模板编辑界面
 */
function openTemplatePage(v) {
	var _projectId = $("#_projectId").val();
	var url = "openTemplatePage.do?template.projectId="+_projectId;
	if (v) {
		url += "&template.templateId=" + v;
	}
	$("#templateDialog").dialog("setTitle", '编辑模板').dialog('open').dialog('refresh', url);
}

/**
 * 保存问卷模板
 */
function submitTemplate() {
	$('#templateForm').form('submit', {
		url : 'editTemplate.do',
		onSubmit : function() {
			return true;
		},
		success : function(data) {
			if(data == "success") {				
				$.messager.alert('成功', '保存成功！', 'info', function() {
					$('#templateDialog').dialog('close');
					$('#templatelList').datagrid('reload');
				});
			} else if(data == "login") {
				window.location.href='index.jsp';
			}
		}
	});
}

/**
 * 删除问卷模板
 */
function delTemplate() {
	var r = $('#templatelList').datagrid('getSelected');
	if(r == null) {
		$.messager.alert('提示', '请选择要删除的记录！', 'info');
	} else {
		$.post('deltetTemplate.do', {"template.templateId":r.templateId}, function(data) {
			if(data == "success") {				
				$.messager.alert('提示', '记录已删除！', 'info', function() {
					$('#templatelList').datagrid('reload');
				});
			} else if(data == "login") {
				window.location.href='index.jsp';
			}
		});
	}
}

/**
 * 启用问卷模板
 */
function enableTemplate() {
	var r = $('#templatelList').datagrid('getSelected');
	if(r == null) {
		$.messager.alert('提示', '请选择记录！', 'info');
	} else {
		$.post('enableTemplate.do', {"template.templateId":r.templateId,"template.projectId":r.projectId}, function(data) {
			if(data == "success") {				
				$.messager.alert('提示', '操作已成功！', 'info', function() {
					$('#templatelList').datagrid('reload');
				});
			} else if(data == "login") {
				window.location.href='index.jsp';
			}
		});
	}
}

/**
 * 生成问卷模板
 */
function loadTemplate() {
	var _projectId = $("#_projectId").val();
	var _templateId = $("#_templateId").val();
	var values = {};
	if(_templateId != "") {
		values = {"template.templateId":_templateId,"template.projectId":_projectId};
	} else {
		values = {"template.projectId":_projectId};
	}
	$.post("findContent.do",values, function(data) {
		$("#loadTemplate").append(data.replace(/@F@/g, "<input type='text'>"));
	});
}

/**
 * 打开样题库界面
 */
function openQuestion(index) {
	var url = "selectQuestionIndex.do?tableId="+index;
	$("#selQuestionDialog").dialog("setTitle", '选择样题').dialog('open').dialog('refresh', url);
}

/**
 * 问卷模板添加样题
 */
function addSelectQuestion() {
	var rows = $("#questionSelectList").datagrid("getSelections");
	if(rows.length == 0) {
		$.messager.alert("提示","请选择样题","info");
		return;
	}
	var ids = [];
	for(var i=0;i<rows.length;i++){
		ids.push(rows[i].questionId);
	}
	ids = ids.join(',');
	if(ids == "") {
		$.messager.alert("提示","请选择样题","info");
		return;
	}
	$.post("searchQuestionByIds.do",{"ids":ids},function(data) {
		var tableId = $("#tableId").val();
		$('#selQuestionDialog').dialog('close');
		// 模板添加样题
		$.each(data, function() {
			var o = this;
			var n = guid();
			var xmlDoc = "<question index='"+tableId+"'>";
			xmlDoc += "<uuid>"+n+"</uuid>";
			xmlDoc += "<questionId>"+o.questionId+"</questionId>";
			xmlDoc += "<questionTitle>"+o.questionTitle+"</questionTitle>";
			xmlDoc += "<questionType>"+o.questionType+"</questionType>";
			xmlDoc += "<questionText></questionText>";
			xmlDoc += "<questionSource></questionSource>";
			xmlDoc += "<use>Y</use>";
			xmlDoc += "<source></source>";
			var t = "<tr id='" + n + "'><td><p>";
			if(o.questionType == "F") {
				var title = o.questionTitle;
				t += title.replace("@F@", "<input type='text'>");
			} else {
				t += o.questionTitle;
			}
			t += "</p>";
			// 判断样题类型
			xmlDoc += "<options>";
			t += "<p>";
			if(o.questionType == "R") {
				// 添加选项
				$.each(o.options, function(index) {
					xmlDoc += "<option selected='N'>";
					t += "<input type='radio' name='" + n + "'>&nbsp;&nbsp;"+this.optionName;
					xmlDoc += this.optionName;
					xmlDoc += "</option>";
				});
			} else if(o.questionType == "C") {
				// 添加选项
				$.each(o.options, function(index) {
					xmlDoc += "<option selected='N'>";
					t += "<input type='checkbox' name='" + n + "'>&nbsp;&nbsp;"+this.optionName;
					xmlDoc += this.optionName;
					xmlDoc += "</option>";
				});
			} else if(o.questionType == "A") {
				t += "<textarea rows='2' cols='40'></textarea>";
			}
			t += "</p>";
			xmlDoc += "</options>";
			xmlDoc += "</question>";
			t += "</td><td align='right' width='135' valign='top'>";
			t += '<input type="hidden" name="questionText" value="'+xmlDoc+'" id="vs' + n + '"/>';
			t += "<input type='text' value='' size='5' onchange='changeSource(\"" + n + "\", this.value)'>";
			t += "<input type='button' value='删除' onclick='delTemplateQuestion(\"" + n + "\")'>";
			t += "<input type='button' value='上移' onclick='moveQuestion(\"" + n + "\", \"up\")'>";
			t += "<input type='button' value='下移' onclick='moveQuestion(\"" + n + "\", \"down\")'>";
			t += "</td></tr>";
			$("#" + tableId).append(t);
		});
	},"json");
}

/**
 * 移除样题
 * @param n
 */
function delTemplateQuestion(n) {
	$("#" +n).remove();
}

/**
 * 设置样题分数
 * @param n
 * @param val
 */
function changeSource(n, val) {
	var vs = $("#vs"+n).val();
	vs = vs.replace(/<input type='text'>/g, "@F@");
	var xmlDoc = loadXML(vs);
	if(document.all) {
		// IE操作XML
		var questionSource = xmlDoc.selectSingleNode("//question/questionSource");
		questionSource.text = val;
		$("#vs"+n).val(xmlDoc.xml);
	} else {
		// Firefox,Chrome等其它浏览器操作XML
		var qsElement = xmlDoc.getElementsByTagName("questionSource")[0];
		var x = qsElement.childNodes[0];
		if(x) {			
			qsElement.removeChild(x);
		}
		qsText = xmlDoc.createTextNode(val);
		qsElement.appendChild(qsText);
		var text = (new XMLSerializer()).serializeToString(xmlDoc);
		$("#vs"+n).val(text);
	}
}

/**
 * 读取xml
 * @param xml
 * @returns
 */
function loadXML(xml) {
	if(document.all) {
		var xmlDom=new ActiveXObject("Microsoft.XMLDOM");
		xmlDom.loadXML(xml);
		return xmlDom;
	} else {
		var xmlDom = (new DOMParser()).parseFromString(xml, "text/xml");
		return xmlDom;
	}
}